<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="职务" prop="projectDuitsName">
            <el-select v-model="queryParams.projectDuitsName" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_duits" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:iecturer:add']" icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd">添加培训员
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainerList" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="培训讲师" prop="staffName"/>
      <el-table-column align="center" label="职务" prop="projectDuitsName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="岗位" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="年龄">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="任职状态">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? '已取消' : '任职' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加日期" prop="iecturerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.iecturerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="已发奖励金额(元)" prop="bonus"/>
      <el-table-column align="center" label="完成培训人数" prop="completeNumber"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:iecturer:query']" size="mini" type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button v-hasPermi="['cld:iecturer:remove']" v-if="scope.row.status == 1" size="mini" type="text" @click="handleCancePosition(scope.row)">取消任职</el-button>
          <el-button v-hasPermi="['cld:iecturer:edit']" v-if="scope.row.status == 0" size="mini" type="text" @click="handleUpdate(scope.row)">恢复任职</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>

    <!-- 查看讲师详情 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="viewOpen" append-to-body title="查看讲师详情" width="70%">
      <analyze :data="form"></analyze>
      <el-form ref="queryFormView" :model="queryParamsView" label-width="68px" size="mini">
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="受训人" prop="staffName">
              <el-input v-model="queryParamsView.staffName" clearable placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="培训状态" prop="recevieStatus">
              <el-select v-model="queryParamsView.recevieStatus" clearable placeholder="请选择">
                <el-option v-for="dict in recevieStatusOptions" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="10px">
              <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQueryView">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQueryView">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="form.trainReceiveList" border>
        <el-table-column align="center" label="项目区域" prop="projectRegion">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" prop="projectName"/>
        <el-table-column align="center" label="员工姓名" prop="staffName"/>
        <el-table-column align="center" label="职务" prop="jobName"/>
        <el-table-column align="center" label="年龄">
          <template slot-scope="scope">
            {{ calculateAge(scope.row.staffIdcard) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="培训类型" prop="trainType">
          <template slot-scope="scope">
            {{ scope.row.trainType == 1 ? '入职培训' : '岗前培训' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="奖励金额" prop="money"/>
        <el-table-column align="center" label="培训状态" prop="recevieStatus">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.recevieStatus == 0" type="info">待提交</el-tag>
            <el-tag v-if="scope.row.recevieStatus == 1" type="danger">待审核</el-tag>
            <el-tag v-if="scope.row.recevieStatus == 2 || scope.row.recevieStatus == 3" type="success">已完成</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加培训员 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="添加培训员" width="70%">
      <selected-staff v-if="open" :params="{ notStaffIds: notStaffIds.join(','), staffStatuss: '2' }" type="trainer" @childInput="handleStaffChange"></selected-staff>
    </el-dialog>
  </div>
</template>

<script>
import {addIecturer,updateIecturer, getTrainIecturerStaffListInfo, listIecturer,delCancePosition} from "@/api/cld/iecturer";
import analyze from "./analyze.vue";
import {recevieStatusOptions} from '@/assets/staticDict'
import selectedStaff from '../staff/selectedStaff.vue';

export default {
  name: "Iecturer",
  dicts: ['cld_project_region', 'cld_project_post', 'cld_project_duits'],
  components: {
    analyze,
    selectedStaff
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训讲师表格数据
      trainerList: [],
      // 受训人列表
      staffList: [],
      // 是否显示弹出层
      open: false,
      // 查看培训讲师
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        projectDuitsName: null
      },
      // 查询参数
      queryParamsView: {
        staffName: null,
        recevieStatus: null,
      },
      // 表单参数
      form: {},
      // 过滤已添加员工
      notStaffIds: [],
      // 受训人状态
      recevieStatusOptions: recevieStatusOptions,
      // 当前选择行
      currRow: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训讲师列表 */
    getList() {
      this.loading = true;
      listIecturer(this.queryParams).then(response => {
        this.trainerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        trainIecturerId: null,
        staffId: null,
        status: null,
        iecturerTime: null,
        unIecturerTime: null,
        createId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQueryView() {
      this.handleGetTrainIecturerStaffListInfo();
    },
    /** 重置按钮操作 */
    resetQueryView() {
      this.resetForm("queryFormView");
      this.handleQueryView();
    },
    // 查看培训讲师详情分页
    handleGetTrainIecturerStaffListInfo() {
      getTrainIecturerStaffListInfo(this.queryParamsView).then(res => {
        this.form = res.data;
      })
    },
    // 查看培训讲师
    handleView(row) {
      this.reset();
      this.currRow = row;
      this.queryParamsView.trainIecturerId = row.trainIecturerId;
      getTrainIecturerStaffListInfo(this.queryParamsView).then(res => {
        this.form = res.data;
        this.viewOpen = true;
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // 获取已添加员工id
      this.notStaffIds = this.trainerList.map(item => item.staffId)
      this.open = true;
      this.title = "添加培训讲师";
    },
    // 培训员选择回调
    handleStaffChange(row) {
      // 新增培训员
      this.form = {
        ...row,
        status: 1,
        iecturerTime: new Date()
      }
      addIecturer(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    // 取消任职
    handleCancePosition(row){
      this.$modal.confirm('是否确认取消["' + row.staffName + '"]的培训员讲师身份？').then(function () {
        return delCancePosition(row.trainIecturerId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    // 恢复任职
    handleUpdate(row){
      this.$modal.confirm('是否确认恢复["' + row.staffName + '"]的培训员讲师身份？').then(function () {
        let form = {
          ...row,
          status: 1
        }
        return updateIecturer(form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    }
  }
};
</script>
